﻿using DemoApp1.Models;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;

namespace DemoApp1.Common
{
    public class RestClientHelp
    {
        private string _resourceServerBaseAddress;
        private string _resourceAPIAddress;
        private RestClient client;

        protected RestClient Client
        {
            get
            {
                if (this.client == null)
                {
                    this.client = new RestClient(_resourceServerBaseAddress);
                }
                return this.client;
            }
        }
        public RestClientHelp(string resourceServerBaseAddress, string resourceAPIAddress)
        {
            this._resourceServerBaseAddress = resourceServerBaseAddress;
            this._resourceAPIAddress = resourceAPIAddress;
        }

        public string AccessToken(string clientId, string clientSecret, string code)
        {
            var request = new RestRequest(AppConfig.TokenPath, Method.POST);
            request.AddParameter("client_id", clientId);
            request.AddParameter("client_secret", clientSecret);
            request.AddParameter("grant_type", AppConfig.GrantType);
            request.AddParameter("redirect_uri", AppConfig.RedirectUri);
            request.AddParameter("code", code);

            IRestResponse response = Client.Execute(request);
            if (response.StatusCode == HttpStatusCode.OK)
            {
                return response.Content;
            }
            else
            {
                return string.Empty;
            }
        }

        public string RefreshToken(string clientId, string clientSecret, string refreshToken)
        {
            var request = new RestRequest(_resourceAPIAddress, Method.POST);
            request.AddParameter("client_id", clientId);
            request.AddParameter("client_secret", clientSecret);
            request.AddParameter("grant_type", AppConfig.GrantTypeForRefreshToken);
            request.AddParameter("redirect_uri", AppConfig.RedirectUri);
            request.AddParameter("refresh_token", refreshToken);

            IRestResponse response = Client.Execute(request);
            if (response.StatusCode == HttpStatusCode.OK)
            {
                return response.Content;
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 根据token调用Server端API，获取用户信息
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public string GetUserData(string token)
        {
            var request = new RestRequest(_resourceAPIAddress, Method.GET);
            request.AddHeader("Authorization", string.Format("bearer {0}", token));
            IRestResponse response = Client.Execute(request);
            if (response.StatusCode == HttpStatusCode.OK)
            {
                return response.Content;
            }
            return string.Empty;
        }
    }
}